include(BundleUtilities)
function(fixup_bundle app libs dirs)
  message(STATUS "fixup_bundle")
  message(STATUS "  app='${app}'")
  message(STATUS "  libs='${libs}'")
  message(STATUS "  dirs='${dirs}'")

  get_bundle_and_executable("${app}" bundle executable valid)
  if(valid)
    get_filename_component(exepath "${executable}" PATH)

    message(STATUS "fixup_bundle: preparing...")
    get_bundle_keys("${app}" "${libs}" "${dirs}" keys)

    list(REMOVE_ITEM keys "Python" "libtcl8_6_dylib" "libtk8_6_dylib") # skip fixup of Python
    foreach(key "Python" "libtcl8_6_dylib" "libtk8_6_dylib") # skip fixup of Python${keys})
	    message("unset ${key}_COPYFLAG")
	    unset(${key}_COPYFLAG)
	    unset(${key}_DEFAULT_EMBEDDED_PATH)
	    unset(${key}_EMBEDDED_ITEM)
	    unset(${key}_ITEM)
	    unset(${key}_RDEP_RPATHS)
	    unset(${key}_RESOLVED_EMBEDDED_ITEM)
	    unset(${key}_RESOLVED_ITEM)
	    unset(${key}_RPATHS)
    endforeach()

    message(STATUS "fixup_bundle: copying...")
    list(LENGTH keys n)
    math(EXPR n ${n}*2)

    get_cmake_property(_variableNames VARIABLES)
    list (SORT _variableNames)
    foreach (_variableName ${_variableNames})
	    message(STATUS "${_variableName}=${${_variableName}}")
    endforeach()

    set(i 0)
    foreach(key ${keys})
      math(EXPR i ${i}+1)
      if(${${key}_COPYFLAG})
        set(item "${${key}_ITEM}")
        if(item MATCHES "[^/]+\\.framework/")
          copy_resolved_framework_into_bundle("${${key}_RESOLVED_ITEM}"
            "${${key}_RESOLVED_EMBEDDED_ITEM}")
        else()
          copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
            "${${key}_RESOLVED_EMBEDDED_ITEM}")
        endif()
      endif()
    endforeach()

    message(STATUS "fixup_bundle: fixing...")
    foreach(key ${keys})
      math(EXPR i ${i}+1)
      message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
      # ignore dependencies to python
      fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")  
    endforeach()

    message(STATUS "fixup_bundle: cleaning up...")
    clear_bundle_keys(keys)

  else()
    message(SEND_ERROR "error: fixup_bundle: not a valid bundle")
  endif()

  message(STATUS "fixup_bundle: done")
endfunction()

get_filename_component(APPNAME @CMAKE_INSTALL_PREFIX@ NAME)
set(APP "${CMAKE_CURRENT_BINARY_DIR}/bundle_dir/${APPNAME}")
set(MACOS_DIR ${APP}/Contents/MacOS)

set(BU_CHMOD_BUNDLE_ITEMS ON)

message("copy @CMAKE_INSTALL_PREFIX@ dir to bundle_dir")
execute_process(COMMAND mkdir bundle_dir)
execute_process(COMMAND cp -r @CMAKE_INSTALL_PREFIX@ bundle_dir)
execute_process(COMMAND ln -s /Applications bundle_dir/Applications)

file(GLOB libs ${MACOS_DIR}/*.dylib ${MACOS_DIR}/*.so)
message("libraries to fix: ${libs}")
fixup_bundle( ${MACOS_DIR}/netgen "${libs}" ${MACOS_DIR} )
message("create image")
execute_process(COMMAND hdiutil create -size 200m -volname NGSolve -srcfolder bundle_dir -ov -format UDZO NGSolve-@NGSOLVE_VERSION@.dmg)
execute_process(COMMAND otool -L ${MACOS_DIR}/netgen)
message("delete bundle_dir")
execute_process(COMMAND rm -rf bundle_dir)
